<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2019/1/10
 * Time: 15:36
 */

namespace api\storage\controller;


use api\storage\common\Constant;
use api\storage\common\InputFactory;
use api\storage\model\SeatModel;
use api\storage\model\ShelvesModel;
use api\storage\model\SubMaterielModel;
use api\storage\service\SocketService;
use think\console\command\make\Model;

class InputController extends AppBaseController
{

    /**
     * @api {GET} /api/storage/input 1.获取站位or校验
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  index
     * @apiGroup  input
     * @apiVersion 0.0.1
     * @apiDescription 对于有灯仓库通过物料号查询空余站位，对于无灯仓库验证物料信息
     *
     * @apiParam {string} reelId 物料条形码
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 获取成功
     * @apiSuccess {Object} data
     * @apiSuccess {string} data.subMaterielId 物料号
     * @apiSuccess {string} data.seatCode 站位信息
     * @apiSuccess {string} data.count 数量
     * @apiError  Error
     **/
    public function index()
    {
        $reelId = input('reelId');
        $userId = $this->getUserId();
        $storageType = $this->getStorageType();
        $service = InputFactory::getInputService($storageType);
        $result = $service->check($reelId, $userId, $this->getStorageUuid());
        switch ($result['status']) {
            case -2:
                $this->error('reelId错误或没有导入入库单');
                break;
            case -1:
                $this->error('C类物料不能放在A、B类货架上');
                break;
            case 0:
                $this->error('没有空的站位');
                break;
            case 1:
                $this->success('获取成功', $result['data']);
                break;
            case 2:
                $this->success('选择物料类型', $result['data'], Constant::RETURN_CODE_MATERIEL_TYPE);
                break;
            case 3:
                $this->success('物料已上架', $result['data'], Constant::RETURN_CODE_INPUT_ON_SHELF);
                break;
            case 4:
                $this->error('未找到该物料信息');
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {POST} /api/storage/input 3.物料入库
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  save
     * @apiGroup  input
     * @apiVersion 0.0.1
     * @apiDescription 对reelId进行入库操作
     *
     * @apiParam {string} seatCode 站位号
     * @apiParam {string} reelId reelId
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 入库成功
     * @apiError  Error
     **/
    public function save()
    {
        $seatCode = input('seatCode');
        $reelId = input('reelId');
        $service = InputFactory::getInputService($this->getStorageType());
        $result = $service->putInfoShelf($reelId, $seatCode, $this->getStorageUuid(), $this->getNickname());
        switch ($result['status']) {
            case -2:
                $this->error('该站位已经被使用');
                break;
            case -1:
                $this->error('该站位不存在');
                break;
            case 0:
                $this->error('入库失败');
                break;
            case 1:
                $this->success('入库成功');
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {PUT} /api/storage/input/type_update 2.1设置物料类型
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  updateType
     * @apiGroup  input
     * @apiVersion 0.0.1
     * @apiDescription 对物料号进行设置类型
     *
     * @apiParam {string} type 货架类型 A | B | C
     * @apiParam {string} materielId 物料号
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 设置成功
     * @apiError  Error
     **/
    public function updateType()
    {
        $type = strtoupper(input('type'));//所有字符换成大写，输入货架类型
        if ($type != Constant::SHELF_TYPE_OF_SMALL && $type != Constant::SHELF_TYPE_OF_BIG && $type != Constant::SHELF_TYPE_OF_CONTAINER) {
            $this->error('货架类型不正确');
        }
        $materielId = input('materielId');
        $materielModel = new SubMaterielModel();
        $res = $materielModel->updateByMaterielId(['shelves_type' => $type], $materielId);
        if ($res) {
            $this->success('设置成功');
        } else {
            $this->error('设置失败');
        }
    }

    /**
     * @api {PUT} /api/storage/input/cancel 2.2取消入库
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  cancel
     * @apiGroup  input
     * @apiVersion 0.0.1
     * @apiDescription 取消入库，如果是已经亮灯的话，将灯灭掉
     *
     * @apiParam {string} seatCode 站位号
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 取消成功
     * @apiError  Error
     **/
    public function cancel()
    {
        $seatCode = input('seatCode');
        $IsBind = input('isBind');
        $seatModel = new SeatModel();
        $seatId = $seatModel->findSeatIdByCodeAndStorageUuid($seatCode, $this->getStorageUuid());
        if ($IsBind == false){//若未绑定，则取消时应清除shelves表的user_id
            $service = InputFactory::getInputService($this->getStorageType());
            $service->unbindShelf($this->getUserId(), $this->getStorageUuid());
        }
        if (!empty($seatId)) {
            SocketService::send($seatId, $this->getStorageUuid(), Constant::TURN_OFF_LIGHT);
        }
        $this->success('取消成功');
    }

    /**
     * @api {GET} /api/storage/input/shelf_look 查看绑定的货架
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  lookingBindShelf
     * @apiGroup  input
     * @apiVersion 0.0.1
     * @apiDescription 查看绑定的货架
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 查找成功
     * @apiSuccess {object} data
     * @apiSuccess {string} data.shelfCode 货架编码
     * @apiError  Error
     **/
    public function lookingBindShelf()
    {
        $service = InputFactory::getInputService($this->getStorageType());
        $result = $service->lookingBindShelf($this->getUserId(), $this->getStorageUuid());
        switch ($result['status']) {
            case 0:
                $this->error('没有绑定的货架');
                break;
            case 1:
                $this->success('查找成功', $result['data']);
                break;
            default:
                $this->error('请求失败');
                break;
        }
    }

    /**
     * @api {PUT} /api/storage/input/shelf_bind 货架绑定
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  bindShelf
     * @apiGroup  input
     * @apiVersion 0.0.1
     * @apiDescription 查看绑定的货架
     *
     * @apiParam {string} seatCode 站位号
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 绑定成功
     * @apiError  Error
     **/
    public function bindShelf()
    {
        $seatCode = input('seatCode');
        $service = InputFactory::getInputService($this->getStorageType());
        $storageUuid = $this->getStorageUuid();
        $result = $service->bindShelf($seatCode, $this->getUserId(), $storageUuid);
        switch ($result['status']) {
            case -4:
                $this->error('您已绑定其他货架，请先解绑');
                break;
            case -3:
                $this->error('站位不存在');
                break;
            case -2:
                $this->error('货架不存在');
                break;
            case -1:
                $this->error('站位已经被他人绑定');
                break;
            case 1:
                $this->success('绑定成功');
                break;
            default:
                $this->error('请求失败');
                break;
        }
    }

    /**
     * @api {DELETE} /api/storage/input/shelf_unbind 解除货架绑定
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  unbindShelf
     * @apiGroup  input
     * @apiVersion 0.0.1
     * @apiDescription 查看绑定的货架
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 解除绑定成功
     * @apiError  Error
     **/
    public function unbindShelf()
    {
        $service = InputFactory::getInputService($this->getStorageType());
        $result = $service->unbindShelf($this->getUserId(), $this->getStorageUuid());
        switch ($result['status']) {
            case 1:
                $this->success('解除绑定成功');
                break;
            default:
                $this->error('请求失败');
                break;
        }
    }
}